{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "0",
   "metadata": {},
   "source": [
    "# 1. SQLite Memory\n",
    "\n",
    "The memory SQLite database can be thought of as a normalized source of truth. The memory module is the primary way PyRIT keeps track of requests and responses to targets and scores. Most of this is done automatically. All Prompt Targets write to memory for later retrieval. All scorers also write to memory when scoring.\n",
    "\n",
    "The schema is found in `memory_models.py` and can be programmatically viewed as follows"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Database Schema:\n",
      "================\n",
      "\n",
      "Table: PromptMemoryEntries\n",
      "--------------------------\n",
      "  id: CHAR(32) NOT NULL\n",
      "  role: VARCHAR NOT NULL\n",
      "  conversation_id: VARCHAR NOT NULL\n",
      "  sequence: INTEGER NOT NULL\n",
      "  timestamp: DATETIME NOT NULL\n",
      "  labels: JSON NOT NULL\n",
      "  prompt_metadata: JSON NOT NULL\n",
      "  targeted_harm_categories: JSON NULL\n",
      "  converter_identifiers: JSON NULL\n",
      "  prompt_target_identifier: JSON NOT NULL\n",
      "  attack_identifier: JSON NOT NULL\n",
      "  response_error: VARCHAR NULL\n",
      "  original_value_data_type: VARCHAR NOT NULL\n",
      "  original_value: VARCHAR NOT NULL\n",
      "  original_value_sha256: VARCHAR NULL\n",
      "  converted_value_data_type: VARCHAR NOT NULL\n",
      "  converted_value: VARCHAR NULL\n",
      "  converted_value_sha256: VARCHAR NULL\n",
      "  original_prompt_id: CHAR(32) NOT NULL\n",
      "\n",
      "Table: EmbeddingData\n",
      "--------------------\n",
      "  id: CHAR(32) NOT NULL\n",
      "  embedding: ARRAY NULL\n",
      "  embedding_type_name: VARCHAR NULL\n",
      "\n",
      "Table: ScoreEntries\n",
      "-------------------\n",
      "  id: CHAR(32) NOT NULL\n",
      "  score_value: VARCHAR NOT NULL\n",
      "  score_value_description: VARCHAR NULL\n",
      "  score_type: VARCHAR NOT NULL\n",
      "  score_category: JSON NULL\n",
      "  score_rationale: VARCHAR NULL\n",
      "  score_metadata: JSON NOT NULL\n",
      "  scorer_class_identifier: JSON NOT NULL\n",
      "  prompt_request_response_id: CHAR(32) NULL\n",
      "  timestamp: DATETIME NOT NULL\n",
      "  task: VARCHAR NULL\n",
      "  objective: VARCHAR NULL\n",
      "\n",
      "Table: SeedPromptEntries\n",
      "------------------------\n",
      "  id: CHAR(32) NOT NULL\n",
      "  value: VARCHAR NOT NULL\n",
      "  value_sha256: VARCHAR NULL\n",
      "  data_type: VARCHAR NOT NULL\n",
      "  name: VARCHAR NULL\n",
      "  dataset_name: VARCHAR NULL\n",
      "  harm_categories: JSON NULL\n",
      "  description: VARCHAR NULL\n",
      "  authors: JSON NULL\n",
      "  groups: JSON NULL\n",
      "  source: VARCHAR NULL\n",
      "  date_added: DATETIME NOT NULL\n",
      "  added_by: VARCHAR NOT NULL\n",
      "  prompt_metadata: JSON NULL\n",
      "  parameters: JSON NULL\n",
      "  prompt_group_id: CHAR(32) NULL\n",
      "  sequence: INTEGER NULL\n",
      "  role: VARCHAR NULL\n",
      "  is_objective: BOOLEAN NULL\n",
      "\n",
      "Table: AttackResultEntries\n",
      "--------------------------\n",
      "  id: CHAR(32) NOT NULL\n",
      "  conversation_id: VARCHAR NOT NULL\n",
      "  objective: VARCHAR NOT NULL\n",
      "  attack_identifier: JSON NOT NULL\n",
      "  objective_sha256: VARCHAR NULL\n",
      "  last_response_id: CHAR(32) NULL\n",
      "  last_score_id: CHAR(32) NULL\n",
      "  executed_turns: INTEGER NOT NULL DEFAULT ScalarElementColumnDefault(0)\n",
      "  execution_time_ms: INTEGER NOT NULL DEFAULT ScalarElementColumnDefault(0)\n",
      "  outcome: VARCHAR NOT NULL DEFAULT ScalarElementColumnDefault('undetermined')\n",
      "  outcome_reason: VARCHAR NULL\n",
      "  attack_metadata: JSON NULL\n",
      "  pruned_conversation_ids: JSON NULL\n",
      "  adversarial_chat_conversation_ids: JSON NULL\n",
      "  timestamp: DATETIME NOT NULL\n",
      "\n",
      "Table: ScenarioResultEntries\n",
      "----------------------------\n",
      "  id: CHAR(32) NOT NULL\n",
      "  scenario_name: VARCHAR NOT NULL\n",
      "  scenario_description: VARCHAR NULL\n",
      "  scenario_version: INTEGER NOT NULL DEFAULT ScalarElementColumnDefault(1)\n",
      "  pyrit_version: VARCHAR NOT NULL\n",
      "  scenario_init_data: JSON NULL\n",
      "  objective_target_identifier: JSON NOT NULL\n",
      "  objective_scorer_identifier: JSON NULL\n",
      "  scenario_run_state: VARCHAR NOT NULL DEFAULT ScalarElementColumnDefault('CREATED')\n",
      "  attack_results_json: VARCHAR NOT NULL\n",
      "  labels: JSON NULL\n",
      "  number_tries: INTEGER NOT NULL DEFAULT ScalarElementColumnDefault(0)\n",
      "  completion_time: DATETIME NOT NULL\n",
      "  timestamp: DATETIME NOT NULL\n"
     ]
    }
   ],
   "source": [
    "from pyrit.memory import SQLiteMemory\n",
    "\n",
    "# Use in-memory database to avoid file corruption issues\n",
    "memory = SQLiteMemory(db_path=\":memory:\")\n",
    "memory.print_schema()\n",
    "\n",
    "memory.dispose_engine()"
   ]
  }
 ],
 "metadata": {
  "jupytext": {
   "cell_metadata_filter": "-all"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
